package designmodel.strategy.收银策略练习.strategies;

import designmodel.strategy.收银策略练习.CashStrategy;

import java.text.MessageFormat;

/**
 * 每满 fullFactor 减 discountAmount
 *
 * @author scylla
 * @version V1.0
 * @project echo -java-base
 * @title 满减策略
 * @package designmodel.strategy.收银策略练习.strategies
 * @since 2021 /12/16 5:03 下午
 */
public class FullMinusStrategy implements CashStrategy {

    //满多少
    private double fullFactor;

    //减多少
    private double discountAmount;

    /**
     * Instantiates a new Full minus strategy.
     */
    public FullMinusStrategy() {

    }

    public static class Builder {

        private FullMinusStrategy fullMinusStrategy;
        //满多少
        private double fullFactor;

        //减多少
        private double discountAmount;

        public Builder() {
            this.fullMinusStrategy = new FullMinusStrategy();
        }

        public Builder fullFactor(double fullFactor) {
            this.fullFactor = fullFactor;
            return this;
        }

        public Builder discountAmount(double discountAmount) {
            this.discountAmount = discountAmount;
            return this;
        }

        public FullMinusStrategy build() {
            this.fullMinusStrategy.discountAmount = discountAmount;
            this.fullMinusStrategy.fullFactor = fullFactor;

            return this.fullMinusStrategy;
        }
    }

    @Override
    public double pay(double cash) {

        System.out.println("====满减====");
        System.out.println(MessageFormat.format("收银{0}元，每满 {1} 减{2}", cash, fullFactor, discountAmount));

        if (cash >= fullFactor) {
            double time = Math.floor(cash / fullFactor);
            return cash - time * discountAmount;
        }

        return cash;
    }
}
